package com.ruoyi.interview.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.interview.domain.vo.DistrictVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.interview.mapper.RcDistrictMapper;
import com.ruoyi.interview.domain.RcDistrict;
import com.ruoyi.interview.service.IRcDistrictService;

/**
 * 地区Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-28
 */
@Service
public class RcDistrictServiceImpl implements IRcDistrictService 
{
    @Autowired
    private RcDistrictMapper rcDistrictMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 查询地区列表
     * 
     *
     * @return 地区
     */
    @Override
    public List<DistrictVo> selectRcDistrictList()
    {
        List<RcDistrict> rcDistricts = rcDistrictMapper.selectRcDistrictList(null);
        ArrayList<DistrictVo> districtVos = new ArrayList<>();
        List<DistrictVo> districtVo2 = redisService.getCacheList("DistrictVo");
        if (districtVo2.isEmpty()){
            for (RcDistrict rcDistrict : rcDistricts) {
                DistrictVo districtVo = new DistrictVo(rcDistrict.getDistrict(), rcDistrict.getDistrict());
                List<RcDistrict> rcDistricts1 = rcDistrictMapper.selectRcDistrictList(rcDistrict.getDistrictId());
                for (RcDistrict district : rcDistricts1) {
                    DistrictVo districtVo1 = new DistrictVo(district.getDistrict(), district.getDistrict());
                    List<RcDistrict> rcDistricts2 = rcDistrictMapper.selectRcDistrictList(district.getDistrictId());
                    for (RcDistrict rcDistrict1 : rcDistricts2) {
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("value",rcDistrict1.getDistrict());
                        map.put("label",rcDistrict1.getDistrict());
                        districtVo1.addChildren(map);
                    }
                    districtVo.addChildren(districtVo1);
                }
                districtVos.add(districtVo);
            }

            redisService.setCacheList("DistrictVo", districtVos);
            return districtVos;
        }else {
            return districtVo2;
        }



    }

    @Override
    public R districtList() {
        List<RcDistrict> list =   rcDistrictMapper.selectRcDistrict();
        System.out.println(list);
        return R.ok(list);
    }
}
